<template>
  <div id="backccont">
    <div class="container">
      <div class="item">
        <h2 style="color: azure;padding: 15px;text-align: center">欢迎来到xxx系统</h2>
      </div>
      <div class="item">
        <h4 style="color: azure;text-align: center">请登录</h4>
      </div>
      <div class="item">
        <t-form style="margin: 10px" :rules="login_rules" ref="form" :data="formData" :colon="true" :label-width="0" @reset="onReset" @submit="onSubmit">
          <t-form-item name="account">
            <t-input v-model="formData.account" clearable placeholder="请输入账户名">
              <template #prefix-icon>
                <desktop-icon />
              </template>
            </t-input>
          </t-form-item>

          <t-form-item name="password">
            <t-input v-model="formData.password" type="password" clearable placeholder="请输入密码">
              <template #prefix-icon>
                <lock-on-icon />
              </template>
            </t-input>
          </t-form-item>
          <t-form-item>
            <el-switch
              v-model="value3"
              inline-prompt
              active-text="是"
              inactive-text="否"
            />
            <span style="color: azure;width: 80px;text-align: center">  记住我</span>
          </t-form-item>
          <t-form-item>
            <t-button theme="primary" type="submit" block>登录</t-button>
          </t-form-item>
          <t-form-item>
            <t-button theme="success" @click="visibleModelessDrag = true" block>注册</t-button>
          </t-form-item>
        </t-form>
      </div>
      <t-dialog
        :closeOnOverlayClick="false"
        v-model:visible="visibleModelessDrag"
        header="账号注册"
        mode="modal"
        :on-confirm="() => (visibleModelessDrag = true)"
      >
        <template #body>
          <div class="item">
            <div>账号：</div>
            <div><t-input v-model="formData.account" clearable placeholder="请输入账户名">
                    <template #prefix-icon>
                      <desktop-icon />
                    </template>
                  </t-input>
            </div>
          </div>
          <div class="item">
            <div>密码：</div>
            <div>
              <t-input v-model="formData.password" type="password" clearable placeholder="请输入密码">
                <template #prefix-icon>
                  <lock-on-icon />
                </template>
              </t-input>
            </div>
          </div>
          <div class="item">
            <div>确认密码：</div>
            <div>
              <t-input v-model="formData.password_confirm" type="password" clearable placeholder="请输入密码">
                <template #prefix-icon>
                  <lock-on-icon />
                </template>
              </t-input>
            </div>
          </div>
          <div class="item">
            <div>部门：</div>
            <div><t-input v-model="formData.department" clearable placeholder="请输入账户名">
              <template #prefix-icon>
                <desktop-icon />
              </template>
            </t-input>
            </div>
          </div>
          <div class="item">
            <div>甲方领导：</div>
            <div><t-input v-model="formData.leader" clearable placeholder="请输入账户名">
              <template #prefix-icon>
                <desktop-icon />
              </template>
            </t-input>
            </div>
          </div>
        </template>
      </t-dialog>
    </div>
  </div>
</template>

<script setup lang="jsx">
import { DesktopIcon, LockOnIcon } from 'tdesign-icons-vue-next'
import { reactive, ref } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { Login } from '@/util/request'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
const formData = reactive({
  account: '',
  password: '',
  password_confirm: '',
  department: '',
  leader: ''
})

const onReset = () => {
  MessagePlugin.success('重置成功')
}

const myRouter = useRouter()
const onSubmit = ({ validateResult, firstError }) => {
  if (validateResult === true) {
    Login(formData).then(res => {
      if (res.code === 200) {
        myRouter.push('/layout')
      } else {
        ElMessage.error('登录失败')
      }
    }).catch(() => {
      console.log('catch')
      formData.account = ''
      formData.password = ''
      ElMessage.info('重新登陆')
      myRouter.push('/')
    })
  } else {
    console.log('Validate Errors: ', firstError, validateResult)
    MessagePlugin.warning(firstError)
  }
}

const value3 = ref(false)

const visibleModelessDrag = ref(false)

// eslint-disable-next-line camelcase
const login_rules = {
  account: [
    { required: true, message: '姓名必填', type: 'error', trigger: 'blur' },
    { required: true, message: '姓名必填', type: 'error', trigger: 'change' },
    { whitespace: true, message: '姓名不能为空' }
  ],
  password: [
    { required: true, message: '密码必填', type: 'error', trigger: 'blur' }
  ]
}
</script>

<style scoped>
  .container{
    width: 400px;
    height: 400px;
    border-radius: 6px;
    padding: 20px;
    align-items: center;
  }
  #backccont {
    background-image: url(../../static/hero-bg.webp);
    background-attachment: fixed;
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
